package gui;
import javax.microedition.lcdui.*;

import logic.LogicHelper;
import logic.MainMidlet;
import logic.TempUserSettings;

/**
 * screen as Form class with fields to change game settings
 * @author Rekin
 *
 */
public class MenuOptions extends Form implements CommandListener, ItemStateListener{

	//private ChoiceGroup touchscreen;
	private ChoiceGroup singleOrMulti;
	private Gauge playersNumberGauge;
	//private Gauge soundLevel;
	private TextField nameField;
	private TextField moneyField;
	private TempUserSettings userSettings;

	/**
	 * constructor
	 * @param set settings that will be changed
	 */
    public MenuOptions(TempUserSettings set){
        super("Options Menu");
        userSettings=set;
        playersNumberGauge=new Gauge("Players number", true, 4,userSettings.getPlayerNumer());
        //soundLevel=new Gauge("Sound level", true, 10, userSettings.getSoundLevel());
//        touchscreen=new ChoiceGroup("Touchscreen", Choice.EXCLUSIVE,
//                new String[]{"Enabled","Disabled"}, null);
        singleOrMulti=new ChoiceGroup("Game type", Choice.EXCLUSIVE,
                new String[]{"Single player","Multi player (bluetooth)" //, "Multi player (internet)" 
        												}, null);
        
        String playerName="";
        
        if(userSettings.getPlayerName().compareTo(LogicHelper.defaultPlayerName) != 0){
        	playerName=userSettings.getPlayerName();
        }
        nameField=new TextField("Player name", playerName, 20, TextField.ANY);
        moneyField = new TextField("Starting money:", "100", 20, TextField.NUMERIC);
//        if(userSettings.isTouchscreenAvailable()){
//        	touchscreen.setSelectedIndex(0, true);
//        }
//        else{
//        	touchscreen.setSelectedIndex(1, true);
//        }
        singleOrMulti.setSelectedIndex(
        		userSettings.getGameType()-LogicHelper.gameTypeSinglePlayer, true);
        
        addCommand(new Command("Save", Command.OK, 1));
        addCommand(new Command("Discard", Command.CANCEL, 2));
        setCommandListener(this);
        setItemStateListener(this);

        append(nameField);
        append(moneyField);
        append(playersNumberGauge);
        //append(touchscreen);
        append(singleOrMulti);
        //append(soundLevel);
    }

    /**
     * handling for user actions
     */
    public void commandAction(Command c, Displayable d){
        if(c.getCommandType()==Command.CANCEL){
            MainMidlet.getMidletInstance().showMainMenu();
        }
        else if(c.getCommandType()==Command.OK){
            setNewTempData();
            MainMidlet.getMidletInstance().showMainMenu();
        }
    }
    
    /**
     * saves the new data to given TempUserSettings class object
     */
    private void setNewTempData(){
//    	boolean touchEnable=false;
    	int gType = singleOrMulti.getSelectedIndex()+LogicHelper.gameTypeSinglePlayer;
//        if(touchscreen.getSelectedIndex()==0)
//            touchEnable=true;
        int playerNum=playersNumberGauge.getValue();
        //int soundLev=soundLevel.getValue();
        String playerNam=nameField.getString();
        if(playerNam.length()==0){
        	playerNam=LogicHelper.defaultPlayerName;
        }
        
        String startingMoneyStr=moneyField.getString();
        int startingMoney=100;
        try{
        	startingMoney=Integer.parseInt(startingMoneyStr);
        }
        catch(NumberFormatException e){
        	startingMoney=100;
        }
    	userSettings.setPlayerName(playerNam);
    	userSettings.setPlayerNumer(playerNum);
//    	userSettings.setTouchscreenAvailable(touchEnable);
    	userSettings.setGameType(gType);
    	//userSettings.setSoundLevel(soundLev);
    	userSettings.setStartingMoney(startingMoney);
    }

    /**
     * handling for user actions
     */
	public void itemStateChanged(Item item) {
		if(item.getLabel().compareTo("Players number") == 0){
			Gauge gItem = (Gauge)item;
			if(gItem.getValue()<2){ 
				//at least 2 players
				gItem.setValue(2);
			}
			if(singleOrMulti.isSelected(1)){ //if multiplayer don't allow to change gauge
				playersNumberGauge.setValue(2);
			}
		}
		if(item.getLabel().compareTo("Game type") == 0){
			if(singleOrMulti.isSelected(1)){ //if multiplayer don't allow to change gauge
				playersNumberGauge.setValue(2);
			}
		}
	}
}
